<template>
  <form class="cmp cmp-obfuscator-decode row">
    <div class="col-5">
      <fieldset>
        <legend>输入</legend>
        <div class="fieldset-content">
          <textarea class="form-control" v-model.trim="inputContent" placeholder="请输入要处理的文本"></textarea>
        </div>
      </fieldset>
    </div>
    <div class="col-2">
      <div class="form-group-list">
        <div class="form-group form-check">
          <input type="checkbox" class="form-check-input" v-model="delDeadCode">
          <label class="form-check-label">删除死代码</label>
        </div>
        <div class="form-group form-check">
          <input type="checkbox" class="form-check-input" v-model="delObfuscatorCode">
          <label class="form-check-label">
            删除模糊器代码<span class="text-danger">(可能误删)</span>
          </label>
        </div>
        <div class="form-group">
          <x-button type="primary" block @click="decrypt">解 密</x-button>
        </div>
      </div>
    </div>
    <div class="col-5">
      <fieldset>
        <legend>输出</legend>
        <div class="fieldset-content">
          <textarea class="form-control" v-model.trim="outputContent" placeholder="处理后的结果" readonly></textarea>
        </div>
      </fieldset>
    </div>
  </form>
</template>

<script>
export default {
  name: 'obfuscator-decode',
  data () {
    return {
      inputContent: '',
      outputContent: '',
      delDeadCode: true,
      delObfuscatorCode: true,
    }
  },
  methods: {
    // 表单校验
    checkForm () {
      let result = true
      if (!this.inputContent) {
        result = false
        this.$toast('请输入要处理的内容')
      }
      return result
    },
    // 解密
    decrypt () {
      if (!this.checkForm()) return
      this.outputContent = ''
      let ast = global.parser.parse(this.inputContent)
      // 字面量解混淆
      console.log('traverse Hex or Unicode String.......')
      global.traverse(ast, global.simplifyLiteral)
      console.log('constantFold.......')
      global.traverse(ast, global.constantFold)
      console.log('delete Repeat Define.......')
      global.traverse(ast, global.deleteRepeatDefine)
      global.traverse(ast, global.SimplifyIfStatement)
      global.traverse(ast, global.standardLoop)
      console.log('resolve Sequence.......')
      global.traverse(ast, global.resolveSequence)
      console.log('global.traverse CallExpress To ToLiteral.......')
      global.traverse(ast, global.CallExpressToLiteral)
      console.log('constantFold.......')
      global.traverse(ast, global.constantFold)
      // object key值Literal
      console.log('Object Preconditioning .......')
      global.traverse(ast, global.keyToLiteral)
      global.traverse(ast, global.preDecodeObject)
      //处理object
      console.log('Object Decode .......')
      global.traverse(ast, global.decodeObject)
      console.log('Control Flow Decoding.......\n')
      global.traverse(ast, global.decodeControlFlow)
      console.log('constantFold.......')
      global.traverse(ast, global.constantFold)
      if (this.delDeadCode) {
        console.log('remove Dead Code.......\n')
        global.traverse(ast, global.removeDeadCode)
        ast = global.parser.parse(global.generator(ast).code)
        global.traverse(ast, global.removeDeadCode)
      }
      global.traverse(ast, global.simplifyLiteral)
      if (this.delObfuscatorCode) {
        // 可能会误删一些代码
        global.traverse(ast, global.deleteObfuscatorCode)
      }
      console.timeEnd('处理完毕，耗时')
      let {code} = global.generator(ast, {jsescOption:{'minimal': true}})
      this.outputContent = code
    },
  }
}
</script>

<style scoped lang="less">
@import "~@/assets/css/_mixins.less";
@import "~@/assets/css/_color.less";
.cmp-obfuscator-decode {
  height: 100%;
  fieldset {
    height: 100%;
    .fieldset-content {
      height: calc(100% - 2em);
      textarea {
        display: block;
        height: 100%;
      }
    }
  }
  .form-group-list {
    height: 100%;
    padding: .5vw;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}
</style>
